package cn.com.cifi.mars.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.cifi.mars.bean.dto.DictionaryTypeDto;
import cn.com.cifi.mars.bean.dto.SysRoleDto;
import cn.com.cifi.mars.bean.vo.sys.req.SaveSysRoleReqParam;
import cn.com.cifi.mars.bean.vo.sys.req.UpdateSysRoleReqParam;
import cn.com.cifi.mars.bean.vo.sys.rsp.SysRoleVo;
import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.constant.IsDisableEnum;
import cn.com.cifi.mars.entity.common.SysRole;
import cn.com.cifi.mars.mapper.mars.SysRoleMapper;
import cn.com.cifi.mars.service.SysRoleService;
import cn.com.cifi.mars.util.UUIDUtils;
import cn.com.cifi.mars.util.json.JsonResultRespUtils;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	/**
	 * 添加角色
	 */
	@Transactional
	@Override
	public int add(SaveSysRoleReqParam param,String userName) {
		SysRole sysRole = new SysRole();
		// 请求 转实体
		BeanUtils.copyProperties(param, sysRole);
		String roleId = UUIDUtils.create();
		sysRole.setRoleId(roleId);
		String roleName = sysRole.getRoleName().trim();
		SysRole roleBean = sysRoleMapper.selectByRoleName(roleName);
		if (roleBean != null) {
			throw new MsgException(CodeEnum.ROLENAME_ERROR);
		}
		sysRole.setCreateby(userName);
		sysRole.setCreateTime(new Date());
		// disabled 是否禁用，0-否，1-是
		sysRole.setDisabled(IsDisableEnum.NO.getKey() + "");
		int num = sysRoleMapper.insertSelective(sysRole);
		return num;
	}
	/**
	 * 修改角色
	 */
	@Transactional
	@Override
	public int update(UpdateSysRoleReqParam param,String userName) {
		SysRole sysRole = new SysRole();
		// 请求 转实体
		BeanUtils.copyProperties(param, sysRole);
		SysRole roleBean = sysRoleMapper.selectByPrimaryKey(sysRole.getRoleId());
		if (roleBean == null) {
			throw new MsgException(CodeEnum.ROLEID_ERROR);
		}
		String roleName = sysRole.getRoleName().trim();
		SysRole roleNameBean = sysRoleMapper.selectByRoleName(roleName);
		if (roleNameBean != null) {
			// 角色已存在,判断角色和当前是否一致
			if (roleNameBean.getRoleId().equals(roleBean.getRoleId())) {
				// 一致 可以修改
			} else {
				// 不一致 不能修改
				throw new MsgException(CodeEnum.ROLENAME_ERROR);
			}
		}
		sysRole.setCreateby(userName);
		sysRole.setCreateTime(roleBean.getCreateTime());
		sysRole.setDisabled(roleBean.getDisabled());
		int num = sysRoleMapper.updateByPrimaryKey(sysRole);
		return num;
	}
	
	/**
	 * 查询角色信息集合分页
	 */
	@Override
	public SysRoleVo queryRolePage(String current,String pageSize) {
		SysRoleVo vo=new SysRoleVo();
		Page<SysRoleDto>pageDto=new Page<>();
		//参数当前页
		pageDto.setCurrent(Integer.parseInt(current));
		//参数每页数量
		pageDto.setSize(Integer.parseInt(pageSize));
		List<SysRoleDto>list= sysRoleMapper.selectRoleList(pageDto);
		vo.setCurrent(pageDto.getCurrent());
		vo.setPages(pageDto.getPages());
		vo.setPageSize(pageDto.getSize());
		vo.setTotal(pageDto.getTotal());
		vo.setList(list);
		return vo;
	}
	
	/**
	 * 根据角色id删除角色信息
	 */
	@Transactional
	@Override
	public int deleteByRoleId(String roleId) {
		int num= sysRoleMapper.deleteByPrimaryKey(roleId);
		return num;
	}

}
